﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;
using System.Threading;
using System.IO;
using System.Web;
using System.Runtime.InteropServices;
using System.IO.Ports;
using System.Security;
using System.Security.Cryptography;

namespace Business
{
    public partial class qianzhixiugaimima : Form
    {
        public static string uid;
        public static string name;
        public static string bianhao;
        public static string yuanmima;

        // /声明INI文件的写操作函数 WritePrivateProfileString()
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

        //声明INI文件的读操作函数 GetPrivateProfileString()
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        private string sPath = null;
        public static string sip;
        public static string zhanghao;

        public qianzhixiugaimima(string suid, string sname, string sbianhao, string szhanghao)
        {
            InitializeComponent();
            uid = suid;
            name = sname;
            bianhao = sbianhao;
  
            zhanghao = szhanghao;

            string path = "";
            path = System.Windows.Forms.Application.StartupPath + path + "\\本机信息.ini";
            string path1 = string.Format(@"{0}", path);
            Ini(path1);


            sip = ReadValue("设置", "IP");
        }
        //ini文件
        public void Ini(string path)
        {
            this.sPath = path;
        }
        public void Writue(string section, string key, string value)
        {
            // section=配置节，key=键名，value=键值，path=路径
            WritePrivateProfileString(section, key, value, sPath);
        }
        public string ReadValue(string section, string key)
        {
            // 每次从ini中读取多少字节
            System.Text.StringBuilder temp = new System.Text.StringBuilder(255);

            // section=配置节，key=键名，temp=上面，path=路径
            GetPrivateProfileString(section, key, "", temp, 255, sPath);
            return temp.ToString();
        }
        //ini文件
        private string Encryption(string express)//加密
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] plaindate = Encoding.Default.GetBytes(express);
                byte[] encryptdate = rsa.Encrypt(plaindate, false);

                return Convert.ToBase64String(encryptdate);
            }
        }
        //
        private string Decrypt(string cip)//解密
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] encryptdate = Convert.FromBase64String(cip);
                byte[] decryptdate = rsa.Decrypt(encryptdate, false);
                return Encoding.Default.GetString(decryptdate);
            }
        }
        public void updatemima()
        {
            string smima = "";
            smima = Encryption(string.Format("{0}{1}{2}{3}", uid, name, bianhao, text_mima.Text));
            Query objquery = new Query();
            string ssql = "";
            ssql = string.Format(@"update  USERE set PASSWORD='{0}' where YHLB='{1}'", smima, zhanghao);
            if (!objquery.UpdateTable(ssql))
            {
                MessageBox.Show("密码修改失败");

            }
            else
            {
                this.Close();
            }
           
          
        }

        private void but_save_Click(object sender, EventArgs e)
        {
            string s =
                        @"  
                        (?=.*[0-9])                         #必须包含数字  
                        (?=.*[a-zA-Z])                      #必须包含小写或大写字母  
                        (?=([\x21-\x7e]+)[^a-zA-Z0-9])      #必须包含特殊符号  
                        .{8,20}                             #至少8个字符，最多20个字符  
                        ";

            System.Text.RegularExpressions.Regex reg = new System.Text.RegularExpressions.Regex(s, System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.IgnorePatternWhitespace);
            if (!reg.IsMatch(text_mima.Text.Trim()))
            {
                MessageBox.Show("密码必须至少8个字符，最多20个字符，\r\n并且至少有1个字母、\r\n至少有1个数字、\r\n至少有1个特殊字符!");
                return;
            }

            if (text_mima.Text == text_cfmima.Text)
            {
                updatemima();
            }
            else
            {
                MessageBox.Show("密码不一致");
            }

        }
        
    }
}
